package uit.edu.upload2server;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import uit.edu.upload2server.*;

public class Upload2serverActivity extends Activity {
    /** Called when the activity is first created. */
	Button btn_upload;
	TextView tx_view;
	String username;
	String name = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn_upload=(Button)findViewById(R.id.btn_upload);
        tx_view=(TextView)findViewById(R.id.view1);
        username = "hailt88";
//        tx_view.setText("home path is:: ");
//        try {
//        	tx_view.setText("home path is:: "+ Webservices.getUserList());
////        	tx_view.setText("home path is:: " + Webservices.getHomePath());
//			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
////			e.printStackTrace();
//			tx_view.setText("ioex: " +e.toString());
//		} catch (XmlPullParserException e) {
//			// TODO Auto-generated catch block
//			tx_view.setText("xmlex: " +e.toString());
////			e.printStackTrace();
//		}
        
        btn_upload.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				tx_view.setText("home path is:: ");
				uploadFile();
			}
		});
    }
    
    public void uploadFile(){
//        String urlString = "http://10.0.2.2/upload.php";
//    	String name = "nicePic.jpg";
//    	String name = "apps-on-Android.pdf";
        String urlString = "http://10.0.2.2:8080/ServerWeb/UploadServlet.jsp";
//        String urlString = "http://10.0.2.2:8080/ServerWeb/uploadSingle.jsp";
//        String urlString = "http://10.0.2.2:8080/ServerWeb/uploadMulti.jsp";
//      String urlString = "http://10.0.2.2:8080/ServerWeb/uploadEx.jsp";
        File sdcard = Environment.getExternalStorageDirectory();
		File dir = new File(sdcard.getAbsolutePath()+"/Download/");
//		File file = new File(dir,name);
		
//		String name = "2CVjaune.jpg";
//        String name1 = "dungthuongtoi.mp3";
//        String name2 = "image.jpg";
        try {
//        	for(int i= 0;i<=3;i++){
//        		if(i==0){
//        			name = "nicePic.jpg";
//        		}else if (i==1){
//        			name = "apps-on-Android.pdf";
//        		}else if (i==2){
//        			name = "nhac.mp3";
//        		}else if (i==3){
//        			name = "comwhere.png";
//        		}
//        		
//        		File file = new File(dir,name);
//        		FileInputStream fis =  new FileInputStream(file);
//                HttpFileUploader htfu = new HttpFileUploader(urlString,username, name);
//                htfu.doStart(fis);  
        		name = "nicePic.jpg";
                File file = new File(dir,name);
        		FileInputStream fis =  new FileInputStream(file);
                HttpFileUploader htfu = new HttpFileUploader(urlString,username, name);
                htfu.doStart(fis);
                Toast.makeText(getBaseContext(), "upload file successed! " + name, Toast.LENGTH_LONG).show();        	
        } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getBaseContext(), "upload failed!!! "+e.toString() , Toast.LENGTH_LONG).show();
        }
    }   
}